( function() {
            //异步验证用户名
    jQuery.validator.addMethod("checkUserName", function(value, element){
        var result = false;
        // 设置同步
        $.ajaxSetup({
            async: false
        });
        var param = {
            name: value
        };
        $.post(basepath + "/fore/user/checkName", param, function(data){
            result = (1 == data);
        });
        // 恢复异步
        $.ajaxSetup({
            async: true
        });
        return result;
    }, "用户名已经存在");    
   
    //异步验证验证码
    jQuery.validator.addMethod("checkCode", function(value, element){
        var result = false;
        // 设置同步
        $.ajaxSetup({
            async: false
        });
        var param = {
            code: value
        };
        $.post(basepath + "/fore/user/checkCode", param, function(data){
            result = (1 == data);
            
        });
        // 恢复异步
        $.ajaxSetup({
            async: true
        });
        return result;
    }, "验证码不正确");

         jQuery.validator.addMethod("isChinese", function(value, element) {       
         return this.optional(element) || /^[\u4e00-\u9fa5]+$/.test(value);       
    }, "只能输入汉字.");  

        jQuery.validator.addMethod("email2", function(value, element, param) {
            return this.optional(element) || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)*(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
        }, "请正确输入您的邮箱地址");
        jQuery.validator.addMethod("userNameCheck", function(value, element, param) {
            return this.optional(element) || /^[a-zA-Z][a-zA-Z0-9_]{4,19}$/.test(value);
        }, "请输入4-20位字母开头的字母或数字和下划线");



        //字符验证
        jQuery.validator.addMethod("stringCheck", function(value, element) {
            return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value);
        }, "只能包括中文字、英文字母、数字和下划线");

        //中文字两个字节
        jQuery.validator.addMethod("byteRangeLength", function(value, element, param) {
            var length = value.length;
            for (var i = 0; i < value.length; i++) {
                if (value.charCodeAt(i) > 127) {
                    length++;
                }
            }
            return this.optional(element) || (length >= param[0] && length <= param[1] );
        }, "长度介于 {0} 和 {1} 之间");
        
        jQuery.validator.addMethod("notEquals", function(value, element, param) {
            
            return this.optional(element) || value!=param;
        }, "不能为空");

        //身份证号码验证
        jQuery.validator.addMethod("isIdCardNo", function(value, element) {
            return this.optional(element) || checkidcard(value);
        }, "请正确输入您的身份证号码");

        //手机号码验证
        jQuery.validator.addMethod("isMobile", function(value, element) {
            var length = value.length;
            var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
            return this.optional(element) || (length == 11 && mobile.test(value));
        }, "请正确填写您的手机号码");

        //电话号码验证
        jQuery.validator.addMethod("isTel", function(value, element) {
            var tel = /^\d{3,4}-?\d{7,9}$/;
            //电话号码格式010-12345678
            return this.optional(element) || (tel.test(value));
        }, "请正确填写您的电话号码");

        //联系电话(手机/电话皆可)验证
        jQuery.validator.addMethod("isPhone", function(value, element) {
            var length = value.length;
            var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
            var tel = /^\d{3,4}-?\d{7,9}$/;
            return this.optional(element) || (tel.test(value) || mobile.test(value));

        }, "请正确填写您的联系电话");

        //邮政编码验证
        jQuery.validator.addMethod("isZipCode", function(value, element) {
            var tel = /^[0-9]{6}$/;
            return this.optional(element) || (tel.test(value));

        }, "请正确填写您的邮政编码");
        
        

    }());
function checkidcard(num) {
    var len = num.length, re;
    if (len == 15)
        re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/);
    else if (len == 18)
        re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\d)$/);
    else {
        //alert("请输入15或18位身份证号,您输入的是 "+len+ "位");
        return false;
    }
    var a = num.match(re);
    if (a != null) {
        if (len == 15) {
            var D = new Date("19" + a[3] + "/" + a[4] + "/" + a[5]);
            var B = D.getYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
        } else {
            var D = new Date(a[3] + "/" + a[4] + "/" + a[5]);
            var B = D.getFullYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
        }
        if (!B) {
            //alert("输入的身份证号 "+ a[0] +" 里出生日期不对！");
            return false;
        }
    }
    return true;
}

